﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFViewDetail.KuaiQi.Others;
using IFClientDLL;
using ZedGraph;
using IFClientDLL.MyCalc.Data.Line;

namespace FrmTryIF.Ctl.Calc.CtlDataImage
{
    public partial class CtlLineTimeSlot : CtlZedGraphBase
    {
        public CtlLineTimeSlot()
        {
            InitializeComponent();
        }
        public override void InitTryIF(ITryIF tryIF)
        {
            base.InitTryIF(tryIF);
            this.Text = "闪电图";
            InitChartGrap(zedGraphControl1.GraphPane);
            tryIF.Calc.DataManager.OnLineChanged_Minute += new Action<MyData_Line_Minute>(DataManager_OnLineChanged_Minute);
        }
        private bool _IsFirstData=false;
        void DataManager_OnLineChanged_Minute(MyData_Line_Minute obj)
        {
            var dt = this.CalcXDataByDateTime(obj.MinuteTime);
            if (_IsFirstData == false)
            {


                zedGraphControl1.GraphPane.YAxis.Scale.Max = (double)obj.ClosePrice+_YStep;
                zedGraphControl1.GraphPane.YAxis.Scale.Min = (double)obj.ClosePrice - _YStep;
                _IsFirstData = true;
            }
            if (_priceLine.Points.Count != 0 && _priceLine.Points[_priceLine.Points.Count - 1].X == dt)
            {
                _priceLine.Points[_priceLine.Points.Count - 1].Y = (double)obj.ClosePrice;
                
            }
            else
            {
                _priceLine.AddPoint(dt, (double)obj.ClosePrice);
                
            }
            if (_avgPriceLine.Points.Count != 0 && _avgPriceLine.Points[_avgPriceLine.Points.Count - 1].X == dt)
            {
                _avgPriceLine.Points[_avgPriceLine.Points.Count - 1].Y = (double)obj.AvgPrice;
            }
            else
            {
                _avgPriceLine.AddPoint(dt, (double)obj.AvgPrice);
            }
            SetYScale(dt);
        }
        
        private void SetYScale(double dt)
        {

            double min = 10000;
            double max = -10000;
            if (dt > 400)
            {

                double mindt = this.CalcXDataByDateTime(XDate.XLDateToDateTime(dt).AddMinutes(-1));
               

                for (int i = _priceLine.Points.Count - 1; i >= 0; i--)
                {
                    
                    if (_priceLine.Points[i].Y > max)
                    {
                        max = _priceLine.Points[i].Y;
                    }
                    if (_priceLine.Points[i].Y < min)
                    {
                        min = _priceLine.Points[i].Y;
                    }
                }
                for (int i = _avgPriceLine.Points.Count - 1; i >= 0; i--)
                {
                   
                    if (_avgPriceLine.Points[i].Y > max)
                    {
                        max = _avgPriceLine.Points[i].Y;
                    }
                    if (_avgPriceLine.Points[i].Y < min)
                    {
                        min = _avgPriceLine.Points[i].Y;
                    }
                }
            }


           

            if (max > zedGraphControl1.GraphPane.YAxis.Scale.Max || min < zedGraphControl1.GraphPane.YAxis.Scale.Min)
            {
                zedGraphControl1.GraphPane.YAxis.Scale.Max += _YStep;
                zedGraphControl1.GraphPane.YAxis.Scale.Min -= _YStep;
            }

            try
            {
                zedGraphControl1.AxisChange();
                zedGraphControl1.Invalidate();
            }
            catch
            {

            }



        }
        private void InitChartGrap(GraphPane grapPane)
        {
            grapPane.Fill = new ZedGraph.Fill(Color.Black);
            grapPane.Chart.Fill = new ZedGraph.Fill(Color.Black);
            grapPane.Chart.Border.Color = Color.Red;
            grapPane.XAxis.Type = AxisType.Date;
            grapPane.XAxis.Scale.Format = "t";
            grapPane.YAxis.MajorGrid.IsVisible = true;
            grapPane.XAxis.MajorGrid.IsVisible = true;
            //grapPane.YAxis.MinorGrid.IsVisible = true;
            grapPane.XAxis.Scale.IsSkipLastLabel = false;
            grapPane.XAxis.Scale.IsSkipFirstLabel = false;
            grapPane.YAxis.Scale.IsSkipFirstLabel = false;
            grapPane.YAxis.Scale.IsSkipLastLabel = false;
            grapPane.YAxis.Scale.FontSpec.Size = 9;
            grapPane.XAxis.Scale.FontSpec.Size = 9;
            grapPane.Y2Axis.Scale.FontSpec.Size = 9;
            grapPane.YAxis.Scale.Format = "n1";
            grapPane.YAxis.Scale.MagAuto = false;
            grapPane.XAxis.Scale.MagAuto = false;
            grapPane.Y2Axis.Scale.MagAuto = false;
            grapPane.Legend.IsVisible = false;
            grapPane.XAxis.Title.IsVisible = false;
            grapPane.YAxis.Title.IsVisible = false;
            grapPane.XAxis.MajorGrid.Color = Color.Red;
            grapPane.XAxis.MajorTic.Color = Color.Red;
            grapPane.YAxis.MajorGrid.Color = Color.Red;
            grapPane.YAxis.MajorTic.Color = Color.Red;
            grapPane.XAxis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.YAxis.Scale.MaxAuto = false;
            grapPane.YAxis.Scale.MinAuto = false;



           
            _priceLine = grapPane.AddCurve("price", new PointPairList(), Color.White);
            _priceLine.Symbol.IsVisible = false;
            _priceLine.IsY2Axis = false;
            _avgPriceLine = grapPane.AddCurve("Avgprice", new PointPairList(), Color.Yellow);
            _avgPriceLine.Symbol.IsVisible = false;
            _avgPriceLine.IsY2Axis = false;
            
            SetXAxis(zedGraphControl1);
            SetXAxisFormat(zedGraphControl1);
        }
      
        
        LineItem _priceLine;
        LineItem _avgPriceLine;
    }
}
